package com.vito.framework.system.group.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vito.framework.base.controller.BaseController;
import com.vito.framework.base.exception.CommonException;
import com.vito.framework.system.group.model.GroupBean;
import com.vito.framework.system.group.service.GroupService;
import com.vito.support.ApiResponse;
import com.vito.support.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***************************************************************
 * Name:        GroupController.java
 * Create At:   2017年8月16日
 * Module:      
 * Description: [分组管理-包括查询、修改、添加、删除等功能]
 * @author      wangjiajia
 * ------------------------------------------------------------
 * 修改历史
 * 序号         日期                       修改人               修改原因
 * 1    2017年8月16日      wangjiajia    
 * 2
***************************************************************/
@RestController
@RequestMapping("${adminPath}/group")
public class GroupController extends BaseController {
    
    @Autowired
    public GroupService groupService;

    /**
     * Create At:   2017年8月16日
     * @author   wangjiajia
     * @param    
     * @return    
     * @description  [获取数据]
     */
    @PostMapping(value = "queryGroupList", consumes = "application/json;charset=UTF-8")
    public ApiResponse queryGroupList(HttpServletRequest request, @RequestBody JSONObject params) throws CommonException {
	    Map<String, Object> paramMap = super.getParamMap(request,params);
	    if (null == paramMap) {
		    return ApiResponse.of(ResultEnum.PARAMS_INVALID);
	    }

	    paramMap.put("param_type", params.getString("param_type"));

		return ApiResponse.ok(groupService.queryGroupTreeList(paramMap));
    }
    
    /**
     * Create At:   2017年8月16日
     * @author   wangjiajia
     * @param    
     * @return   String 
     * @description  [保存数据]
     */
    @PostMapping(value = "addGroupSave", consumes = "application/json;charset=UTF-8")
    public ApiResponse addGroupSave(HttpServletRequest request, @RequestBody JSONObject params,GroupBean groupBean) {
	    Map<String, Object> paramMap = super.getParamMap(request,params);
	    if (null == paramMap) {
		    return ApiResponse.of(ResultEnum.PARAMS_INVALID);
	    }

        try {
            groupBean.setParentId(params.getString("param_parentId"));
            groupService.addGroupSave(groupBean);
            return ApiResponse.ok(ResultEnum.OK);
        }catch (CommonException e) {
            return ApiResponse.of(e);
        }
    }
    
    /**
     * Create At:   2017年8月16日
     * @author   wangjiajia
     * @param    
     * @return   String 
     * @description  [删除数据]
     */
    @PostMapping(value = "deleteGroup", consumes = "application/json;charset=UTF-8")
    public ApiResponse deleteGroup(HttpServletRequest request, @RequestBody JSONObject params) {
	    Map<String, Object> paramMap = super.getParamMap(request,params);
	    if (null == paramMap) {
		    return ApiResponse.of(ResultEnum.PARAMS_INVALID);
	    }

        try {
            String id = params.getString("selectGroupId");
            if (null == id || "".equals(id)) {
                return ApiResponse.of(ResultEnum.PARAMS_INVALID);
            }
            GroupBean groupBean = groupService.getById(id);
            List<GroupBean> childrenCount = groupService.list(new LambdaQueryWrapper<GroupBean>().eq(GroupBean::getParentId, groupBean.getId()));
            if (childrenCount == null || childrenCount.size() == 0) {
                return ApiResponse.of(ResultEnum.FAIL);
            }
            groupService.removeById(id);
            return ApiResponse.ok(ResultEnum.OK);
        } catch (CommonException e) {
            e.printStackTrace();
            return ApiResponse.of(e);
        }
    }

    /**
     * Create At:   2017年8月16日
     * @author   wangjiajia
     * @param    
     * @return   String 
     * @description  [跳转修改页面]
     */
    @PostMapping(value = "editGroup", consumes = "application/json;charset=UTF-8")
    public ApiResponse editGroup(HttpServletRequest request, @RequestBody JSONObject params) {
	    Map<String, Object> paramMap = super.getParamMap(request,params);
	    if (null == paramMap) {
		    return ApiResponse.of(ResultEnum.PARAMS_INVALID);
	    }

	    Map<String, Object> resMap = new HashMap<>();
	    try {
            String groupId = params.getString("selectGroupId");
            if (null == groupId || "".equals(groupId)) {
                return ApiResponse.of(ResultEnum.PARAMS_INVALID);
            }
		    GroupBean groupBean = groupService.getById(groupId);
            if (null == groupBean) {
                groupBean = new GroupBean();
                groupBean.setParentId("0");
            }
            resMap.put("groupBean", groupBean);
            return ApiResponse.ok(resMap);
        } catch (CommonException e) {
            e.printStackTrace();
            return ApiResponse.of(e);
        }
    }

    /**
     * Create At:   2017年8月16日
     * @author   wangjiajia
     * @param    
     * @return   String 
     * @description  [保存修改数据]
     */
    @PostMapping(value = "editGroupSave", consumes = "application/json;charset=UTF-8")
    public ApiResponse editGroupSave(HttpServletRequest request, @RequestBody JSONObject params ,GroupBean groupBean) {
	    Map<String, Object> paramMap = super.getParamMap(request,params);
	    if (null == paramMap) {
		    return ApiResponse.of(ResultEnum.PARAMS_INVALID);
	    }
        try {
            groupService.updateById(groupBean);
            return ApiResponse.ok(ResultEnum.OK);
        } catch (CommonException e) {
            return ApiResponse.of(e);
        }
    }
}